#!/bin/bash

base=$(cd $(dirname $0);pwd)
script_home=${base%/script*}
config="${script_home%/*}/config.yml"
custom="${script_home%/*}/custom_result.yml"
nacos_path="${script_home%/common*}/snc_product/nacos"
common_path="${nacos_path}/COMMON_GROUP"
default_path="${nacos_path}/DEFAULT_GROUP"

#解压缩
cd ${nacos_path}
rm -rf COMMON_GROUP
rm -rf DEFAULT_GROUP
tar -zxvf ${nacos_path}/nacos_tmp.tar.gz

#修改zk
zookeeper_addr=$(grep '^zookeeper_addr:' "$custom" | awk '{print $2}' | tr -d \"\')
if [ -n "$zookeeper_addr" ]; then
    sed -i "/^application.zookeeper.connect-string/capplication.zookeeper.connect-string=${zookeeper_addr}"  ${common_path}/zookeeper.properties
fi
zookeeper_en_pwd=$(grep '^zookeeper_en_pwd:' "$custom" | awk '{print $2}' | tr -d \"\')
if [ -n "$zookeeper_en_pwd" ]; then
    sed -i "/^application.zookeeper.digest/capplication.zookeeper.digest=${zookeeper_en_pwd}" ${common_path}/zookeeper.properties
fi

#修改kafka
kafka_addr=$(grep '^kafka_addr:' "$custom" | awk '{print $2}' | tr -d \"\')
if [ -n "$kafka_addr" ]; then
    sed -i "/^spring.kafka.bootstrap-servers/cspring.kafka.bootstrap-servers=${kafka_addr}" ${common_path}/kafka.properties
    sed -i "/^application.kafka.bootstrap-servers/capplication.kafka.bootstrap-servers=${kafka_addr}" ${common_path}/kafka.properties
fi

#修改whitelist
get_ip=$(grep -E '^- ' ${config} | awk -F';' '{print $1}'| awk '{print $NF}' | xargs | sed 's/ /,/g')
all_ip="${get_ip},127.0.0.1,::1"
if [ -n "$all_ip" ]; then
    sed -i "/^api.security.whitelist/capi.security.whitelist=${all_ip}" ${common_path}/whitelist.properties
fi

#修改redis
redis_mode=$(grep '^redis_mode:' "$custom" | awk '{print $2}' | tr -d \"\')
redis_ip=$(grep '^redis_ip:' "$custom" | awk '{print $2}' | tr -d \"\')
redis_addr=$(grep '^redis_addr:' "$custom" | awk '{print $2}' | tr -d \"\')
redis_en_pwd=$(grep '^redis_en_pwd:' "$custom" | awk '{print $2}' | tr -d \"\')
if [ "${redis_mode}" == "single" ]; then
    sed -i "/^#spring.redis.host/cspring.redis.host=${redis_ip}" ${common_path}/redis.properties
    sed -i "/^spring.redis.cluster.nodes/c#spring.redis.cluster.nodes=" ${common_path}/redis.properties
    sed -i "/^spring.redis.password/cspring.redis.password=${redis_en_pwd}" ${common_path}/redis.properties
else
    sed -i "/^spring.redis.cluster.nodes/cspring.redis.cluster.nodes=${redis_addr}" ${common_path}/redis.properties
    sed -i "/^spring.redis.password/cspring.redis.password=${redis_en_pwd}" ${common_path}/redis.properties
fi

#修改mysql
mysql_ip=$(grep '^mysql_ip:' "$custom" | awk '{print $2}' | tr -d \"\')
mysql_user=$(grep '^mysql_db_en_user:' "$custom" | awk '{print $2}' | tr -d \"\')
mysql_pwd=$(grep '^mysql_db_en_pwd:' "$custom" | awk '{print $2}' | tr -d \"\')
if [ -n "$mysql_ip" ]; then
    sed -i "s/172.16.100.201/${mysql_ip}/g" ${common_path}/mysql.properties
    sed -i "s/shsnc/${mysql_user}/g" ${common_path}/mysql.properties
    sed -i "s/ENC(CxoVfCMSu7ZlIQI17MJu0ZBUo6HooevN)/${mysql_pwd}/g" ${common_path}/mysql.properties
fi

#修改minio
minio_ip=$(grep '^minio_ip:' "$custom" | awk '{print $2}' | tr -d \"\')
minio_user=$(grep '^minio_access_en_key:' "$custom" | awk '{print $2}' | tr -d \"\')
minio_pwd=$(grep '^minio_secret_en_key:' "$custom" | awk '{print $2}' | tr -d \"\')
if [ -n "$minio_ip" ]; then
    sed -i "s/172.16.100.201/${minio_ip}/g" ${common_path}/minio.properties
    sed -i "s/minioadmin/${minio_user}/g" ${common_path}/minio.properties
    sed -i "s/ENC(qYoSEhzQ1dlJiHCEfwLpAfacpbYj+jGg)/${minio_pwd}/g" ${common_path}/minio.properties
fi

#修改ftp
sftp_ip=$(grep '^sftp_ip:' "$custom" | awk '{print $2}' | tr -d \"\')
sftp_user=$(grep '^sftp_en_user:' "$custom" | awk '{print $2}' | tr -d \"\')
sftp_pwd=$(grep '^sftp_en_pwd:' "$custom" | awk '{print $2}' | tr -d \"\')
if [ -n "$sftp_ip" ]; then
    sed -i "s/172.16.100.201/${sftp_ip}/g" ${common_path}/ftp.properties
    sed -i "s/shsnc/${sftp_user}/g" ${common_path}/ftp.properties
    sed -i "s/ENC(VhUbPVN9TTnSKKV61FS7PCmlgBIaputI)/${sftp_pwd}/g" ${common_path}/ftp.properties
fi

#修改elasticsearch
es_mode=$(grep '^es_mode:' "$custom" | awk '{print $2}' | tr -d \"\')
es_ip=$(grep '^es_node1_ip:' "$custom" | awk '{print $2}' | tr -d \"\')
es_addr=$(grep '^es_addr:' "$custom" | awk '{print $2}' | tr -d \"\')
es_user=$(grep '^es_xpack_en_user:' "$custom" | awk '{print $2}' | tr -d \"\')
es_pwd=$(grep '^es_xpack_en_pwd:' "$custom" | awk '{print $2}' | tr -d \"\')
if [ "${es_mode}" == "single" ]; then
    sed -i "/^spring.elasticsearch.rest.uris/cspring.elasticsearch.rest.uris=${es_ip}" ${common_path}/elasticsearch.properties
    sed -i "/^spring.elasticsearch.rest.username/cspring.elasticsearch.rest.username=${es_user}" ${common_path}/elasticsearch.properties
    sed -i "/^spring.elasticsearch.rest.password/cspring.elasticsearch.rest.password=${es_pwd}" ${common_path}/elasticsearch.properties
else
    sed -i "/^spring.elasticsearch.rest.uris/cspring.elasticsearch.rest.uris=${es_addr}" ${common_path}/elasticsearch.properties
    sed -i "/^spring.elasticsearch.rest.username/cspring.elasticsearch.rest.username=${es_user}" ${common_path}/elasticsearch.properties
    sed -i "/^spring.elasticsearch.rest.password/cspring.elasticsearch.rest.password=${es_pwd}" ${common_path}/elasticsearch.properties
fi

#修改ck
ck_mode=$(grep '^ck_mode:' "$custom" | awk '{print $2}' | tr -d \"\')
ck_ip=$(grep '^ck_ip:' "$custom" | awk '{print $2}' | tr -d \"\')
ck_addr=$(grep '^ck_addr:' "$custom" | awk '{print $2}' | tr -d \"\')
ck_clm=$(grep '^ck_cluster_name:' "$custom" | awk '{print $2}' | tr -d \"\')
ck_pwd=$(grep '^ck_default_en_password:' "$custom" | awk '{print $2}' | tr -d \"\')
if [ "${ck_mode}" == "single" ]; then
    sed -i "s#172.16.100.71#${ck_ip}#g" ${common_path}/clickhouse.properties
    sed -i "s#cluster_3shards_1replicas# #g" ${common_path}/clickhouse.properties
    sed -i "s#ENC(CxoVfCMSu7ZlIQI17MJu0ZBUo6HooevN)#${ck_pwd}#g" ${common_path}/clickhouse.properties
else
    sed -i "s#172.16.100.71:8123#${ck_addr}#g" ${common_path}/clickhouse.properties
    sed -i "s#cluster_3shards_1replicas#${ck_clm}#g" ${common_path}/clickhouse.properties
    sed -i "s#ENC(CxoVfCMSu7ZlIQI17MJu0ZBUo6HooevN)#${ck_pwd}#g" ${common_path}/clickhouse.properties
fi

#修改snc-aiops.properties
os_general_user=$(grep '^os_general_user:' "$custom" | awk '{print $2}' | tr -d \"\')
nginx_ip=$(grep '^nginx_ip:' "$custom" | awk '{print $2}' | tr -d \"\')
hadoop_zk_addr=$(grep '^hadoop_zk_addr:' "$custom" | awk '{print $2}' | tr -d \"\')
hadoop_master_ip=$(grep '^hadoop_master_ip:' "$custom" | awk '{print $2}' | tr -d \"\')
hadoop_master_hostname=$(grep '^hadoop_master_hostname:' "$custom" | awk '{print $2}' | tr -d \"\')
sed -i "s/shsnc/${os_general_user}/g" ${default_path}/snc-aiops.properties
#修改snc-base-service.properties
sed -i "s/172.16.100.201/${nginx_ip}/g" ${default_path}/snc-base-service.properties
#snc-datacenter.properties
sed -i "s/172.16.100.201/${nginx_ip}/g" ${default_path}/snc-datacenter.properties
#snc-platform-manager.properties
sed -i "s/172.16.100.201/${nginx_ip}/g" ${default_path}/snc-platform-manager.properties
#snc-ng-server.properties
sed -i "s/172.16.100.201/${nginx_ip}/g" ${default_path}/snc-ng-server.properties
sed -i "s#/shsnc/#/${os_general_user}/#g" ${default_path}/snc-ng-server.properties
#snc-dev-center-addon.properties
sed -i "s#/shsnc/#/${os_general_user}/#g" ${default_path}/snc-dev-center-addon.properties
#snc-platform-node.properties
sed -i "s#172.16.100.71:2181#${hadoop_zk_addr}#g" ${default_path}/snc-platform-node.properties
sed -i "s#hadoop01#${hadoop_master_hostname}#g" ${default_path}/snc-platform-node.properties
sed -i "s#172.16.100.74#${hadoop_master_ip}#g" ${default_path}/snc-platform-node.properties
